package com.sixth.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sixth.dao.LoginmsgDao;
import com.sixth.dao.UserDao;
import com.sixth.entity.Loginmsg;
import com.sixth.entity.User;
import com.sixth.service.UserService;
import com.sixth.util.CommonResult;
import com.sixth.util.TableLayUi;
import com.sixth.util.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @program: carrent
 * @description:
 * @author: 高振杨
 * @create: 2022-01-12 11:18
 **/
@Service(value = "userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao dao;
    @Autowired
    private LoginmsgDao loginmsgDao;
    @Override
    //登录
    public CommonResult findByLoginnameAndPwd(String loginname, String pwd, HttpServletRequest request) {
        User user=dao.findByNameAndPwd(loginname,pwd);
        String ipAddr = WebUtil.getIpAddr(request);
        if (user==null){
            return new CommonResult(5000,"用户名或密码错误",null);
        }
        //存储登陆对象，在任意地方可以用session获取

        Loginmsg loginmsg = new Loginmsg();

        String logname = user.getLoginname()+"-"+user.getRealname();

        loginmsg.setLoginip(ipAddr);
        loginmsg.setLoginname(logname);
        loginmsg.setLogintime(new Date());
        loginmsgDao.insert(loginmsg);
        WebUtil.getSession().setAttribute("user",user);
        return new CommonResult(2000,"登陆成功",null);
    }

    @Override
    //查询用户
    public TableLayUi listByCondition(int page, int limit, String realname, String loginname, String address, String phone,String identity, Integer sex) {
        //System.out.println(page+limit+realname+loginname+address+phone+identity+sex);
        PageHelper.startPage(page,limit);
        List<User> users=dao.listByCondition(realname,loginname,address,phone,identity,sex);
        PageInfo<User> pageInfo = new PageInfo<User>(users);
        //pageInfo.getTotal:获取的为表中总条数
        //pageInfo.getList():获取的为当前页的记录数据
        return new TableLayUi(0,"",pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    //添加用户
    public CommonResult AddUser(String loginname,String identity, String realname, Integer sex, String address, String phone,String pwd,String position,Integer type) {

        //System.out.println(realname + loginname + address+ phone+ identity+ sex+ pwd+ type+ position);

        int i =dao.adduser(loginname,identity,realname,sex,address,phone,pwd,position,type);
        if (i>0){
            return new CommonResult(2000,"添加成功",null);
        }
        return  new CommonResult(5000,"添加失败",null);
    }

    @Override
    //修改用户信息
    public CommonResult editUser(Integer userid, String loginname, String identity, String realname, Integer sex, String address, String phone, Integer type) {

        int i =dao.update(loginname,identity,realname,sex,address,phone,type,userid);
        if (i>0){
            return new CommonResult(2000,"修改成功",null);
        }
        return  new CommonResult(5000,"修改失败",null);
    }

    @Override
    //修改密码
    public CommonResult updatePassword(Integer userid, String pwd) {
        int i=dao.uppwd(pwd,userid);
        if (i>0){
            return new CommonResult(2000,"修改成功",null);
        }
        return  new CommonResult(5000,"修改失败",null);
    }

    @Override
    //删除
    public CommonResult deleteUser(Integer userid) {

        int i = dao.deleteByPrimaryKey(userid);
        if (i>0){
            return new CommonResult(2000,"删除成功",null);
        }
        return  new CommonResult(5000,"删除失败",null);
    }

    @Override
    //批量删除
    public CommonResult batch(String user) {
        List<User> users = JSONArray.parseArray(user, User.class);
        User u = new User();
        for (int i = 0; i < users.size(); i++) {
            u.setUserid(users.get(i).getUserid());
            Integer uid = u.getUserid();
            int j = dao.dels(uid);
            if (j == 0) {
                return new CommonResult(5000, "操作失败", null);
            }
        }
        return new CommonResult(2000,"操作成功",null);
    }


}
